package org.dreamwork.jasmine2.web.controls;

import org.dreamwork.jasmine2.configure.JasmineConfig;
import org.dreamwork.jasmine2.events.IClickListener;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.events.ClickableDelegate;
import org.dreamwork.jasmine2.engine.IClickable;
import org.dreamwork.jasmine2.engine.HttpContext;
import org.dreamwork.jasmine2.i18n.I18nUtil;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.web.IWebContainer;
import org.dreamwork.util.StringUtil;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-2-4
 * Time: 15:36:22
 */
public class Button extends WebControl implements IClickable {
    private String text;
    private ClickableDelegate delegate = new ClickableDelegate ();

    // life circle
    public void init () {
        htmlTagName = "input";
        super.init ();

        if (!isContainsAttribute ("type"))
            setAttribute ("type", "button");
        if (runAtServer)
            page.registerClientScriptBlock (InternalJS.class, InternalJS.ON_CLICK_JS);
    }

    public String getText () {
        return text;
    }

    public void setText (String text) {
        this.text = text;
        attrs.setProperty ("value", text);
    }

    @Override
    public void onPrerender (IWebControl sender, HttpContext context) throws EventException {
        if (runAtServer) {
            page.registerClientScriptBlock (InternalJS.class, InternalJS.ON_CLICK_JS);
            String onclick = getAttribute ("onclick");
            if (onclick != null) {
                onclick = onclick.replaceAll ("\"", "\\\"").replaceAll ("'", "\\'");
            }
            String onsubmit = null;
            IWebContainer form = page.getForm ();
            if (form != null) {
                onsubmit = form.getAttribute ("onsubmit");
                if (onsubmit != null)
                    onsubmit = onsubmit.replaceAll ("\"", "\\\"").replaceAll ("'", "\\'");
            }

            StringBuilder expression = new StringBuilder ();
            if (onclick != null && !"__client_stub (this, 'click', null)".equals (onclick)) expression.append (onclick);
            if (expression.length () > 0) expression.append (';');
            if (onsubmit != null) expression.append ("if (").append (onsubmit).append (")");
            expression.append ("__client_stub (this, 'click', null)");
            attrs.setProperty ("onclick", expression.toString ());
        }

        String text = getAttribute ("value");
        if (!StringUtil.isEmpty (text)) {
            if (context.getApplication ().getAttribute (JasmineConfig.JASMINE_I18N_HOLDER) != null)
                text = I18nUtil.eval (text, this);
            text = I18nUtil.escapeI18n (text);
        }
        if (text != null) setAttribute ("value", text);
    }

    // events

    public void addClickListener (IClickListener listener) {
        delegate.addClickListener (listener);
    }

    public void removeClickListener (IClickListener listener) {
        delegate.removeClickListener (listener);
    }

    public void fireControlClicked (IWebControl sender) throws EventException {
        delegate.fireControlClicked (sender);
    }

    public IClickListener[] getClickListeners () {
        return delegate.getClickListeners ();
    }
}